<template>
  <div>
    <el-form :inline="true" class="demo-form-inline" :model="cForm">
      <el-form-item label="一级分类">
        <el-select v-model="cForm.category1Id" :disabled="show" placeholder="请选择" @change="handler1">
          <el-option v-for="c1 in list1" :key="c1.id" :label="c1.name" :value="c1.id" />

        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select v-model="cForm.category2Id" :disabled="show" placeholder="请选择" @change="handler2">
          <el-option v-for="c2 in list2" :key="c2.id" :label="c2.name" :value="c2.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select v-model="cForm.category3Id" :disabled="show" placeholder="请选择" @change="handler3">
          <el-option v-for="c3 in list3" :key="c3.id" :label="c3.name" :value="c3.id" />
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'CategorySelect',
  props: ['show'],
  data() {
    return {
      // 一级分类
      list1: [],
      // 二级分类
      list2: [],
      // 三级分类
      list3: [],
      // 收集各级分类的ID
      cForm: {
        category1Id: '',
        category2Id: '',
        category3Id: ''
      }
    }
  },
  // 组件挂载完毕获取一级分类数据
  mounted() {
    this.getCategoryList()
  },
  methods: {
    onSubmit() {
      console.log('submit!')
    },
    // 获取一级分类数据
    async getCategoryList() {
      const res = await this.$API.attr.reqCategoryList1()
      if (res.code === 200) {
        this.list1 = res.data
      }
    },
    // 一级分类值改变时,获取二级分类
    async handler1() {
      // 清除二级分类 三级分类数据
      this.list2 = []
      this.list3 = []
      this.cForm.category2Id = ''
      this.cForm.category3Id = ''
      const { category1Id } = this.cForm
      this.$emit('getCategoryId', { categoryId: category1Id, level: 1 })
      const res = await this.$API.attr.reqCategoryList2(category1Id)
      if (res.code === 200) {
        this.list2 = res.data
      }
    },
    // 二级分类值改变时,获取三级分类
    async handler2() {
      // 清除 三级分类数据
      this.list3 = []
      this.cForm.category3Id = ''
      const { category2Id } = this.cForm
      this.$emit('getCategoryId', { categoryId: category2Id, level: 2 })
      const res = await this.$API.attr.reqCategoryList3(category2Id)
      if (res.code === 200) {
        this.list3 = res.data
      }
    },
    // 三级分类事件
    handler3() {
      const { category3Id } = this.cForm
      this.$emit('getCategoryId', { categoryId: category3Id, level: 3 })
    }
  }
}
</script>

<style>
</style>
